<?
include_once(ROOT_PATH_ . "/function/get_cat_child.php");

//接收传递过来的分类id
$trans_nid   = addslashes($_REQUEST ['nid']);

//接收传递过来的板块信息
$system_part = addslashes($_REQUEST ['sys_part']);

$where = '';
$keywords = isset($_REQUEST ['select_keywords']) ? addslashes($_REQUEST ['select_keywords']) : '';

if($keywords != ''){
	$where = " and (name like '%%$keywords%%' or model  like '%%$keywords%%' ) ";
}

$cat_id = isset($_REQUEST ['cat_id']) ? intval($_REQUEST ['cat_id']) : 0;

$brand_id = isset($_REQUEST ['brand_id']) ? intval($_REQUEST ['brand_id']) : 0;

if($cat_id > 0 ){
	
	 $trans_nid = $cat_id;
	 
}
//分类id存在，则说明要搜索板块中某一个分类的产品。
//统计数目就是统计relation表格，因为relation有term_id项
if ($trans_nid > 0 || $brand_id > 0 ) {

//设置需要搜索的表格的前缀后缀
    $table_Suffix = "relation";
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//设置需要搜索的表格的排序项和升降项
    $sort_by = "nid";
    $sort_order = "desc";
//设置需要搜索的表格的排序项和升降项
	
	$id_where = '';

	if($trans_nid > 0){
		
		$cat_list = get_cat_child($trans_nid,$system_part);
		
	}
	
	if($brand_id > 0){
		
		$brand_list = get_cat_child($brand_id,'brand');
		
	}
	
	if($brand_list && $cat_list){ //加入2个都有  取相同的值
		
		
		$cresult = mysql_query("select * from ".$table_Prefix."relation where term_nid in ( ".implode(',',$cat_list)." ) ");
	
		while ($crow = mysql_fetch_array($cresult)) {
			$cat_relation_nid[] = $crow['detail_nid'];
		}
		
		$bresult = mysql_query("select * from ".$table_Prefix."relation where term_nid in ( ".implode(',',$brand_list)." ) ");
	
		while ($brow = mysql_fetch_array($bresult)) {
			$brand_relation_nid[] = $brow['detail_nid'];
		}
		
		$relation_id_list=array_intersect($brand_relation_nid,$cat_relation_nid);
		
		$id_where = " and detail_nid in ( ".implode(',',$relation_id_list)." ) ";
	}
	elseif($brand_list){
		
		$bresult = mysql_query("select * from ".$table_Prefix."relation where term_nid in ( ".implode(',',$brand_list)." ) ");
	
		while ($brow = mysql_fetch_array($bresult)) {
			$relation_id_list[] = $brow['detail_nid'];
		}
		
		$id_where = " and detail_nid in ( ".implode(',',$relation_id_list)." ) ";
	}
	elseif($cat_list){
		
		$cresult = mysql_query("select * from ".$table_Prefix."relation where term_nid in ( ".implode(',',$cat_list)." ) ");
	
		while ($crow = mysql_fetch_array($cresult)) {
			$relation_id_list[] = $crow['detail_nid'];
		}
		
		$id_where = " and detail_nid in ( ".implode(',',$relation_id_list)." ) ";
	}
    
	
	//$query = mysql_query("SELECT COUNT(nid) FROM $table where 1 $id_where ");
    $sum = count($relation_id_list);

}
else {

//设置需要搜索的表格的前缀后缀
    $table_Suffix = "detail";
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//设置需要搜索的表格的排序项和升降项
    $sort_by = "nid";
    $sort_order = "desc";
//设置需要搜索的表格的排序项和升降项

    $query = mysql_query("SELECT COUNT(nid) FROM $table where belong = '$system_part' $where ");
    $sum = mysql_result($query, 0);

}


//设置翻页代码相关内容

$nowpageid = 1;
$detailnum = 10; //每页显示多少个产品


if ($_REQUEST ['nowpageid']) {
    $nowpageid = $_REQUEST ['nowpageid'];
} else {
    $nowpageid = 1;
}


//计算一下结果，从那条数据开始显示
$detailstar = ($nowpageid - 1) * $detailnum;


if ($sum) {

    if ($sum < $detailnum) {
        $page_count = 1;
        $detailnum = $sum;
    } //如果总数据量小于$Pagenum,（pagenum作为每页显示数据的条数，）那么只有一页,而pagenum也更改为sum 


    else if ($sum % $detailnum) {
        $page_count = (int)($sum / $detailnum) + 1;
    }
    //取总数据量除以每页数的余数 
    //如果有余数，则页数等于总数据量除以每页数的结果取整再加一


    else {
        $page_count = $sum / $detailnum;
    }
    //如果没有余数，则页数等于总数据量除以每页数的结果 

} else {
    $page_count = 0;
    $detailnum = 0;
}


$lastpage = $page_count;
$prepage = $nowpageid - 1;
$nextpage = $nowpageid + 1;


if ($nowpageid == 1) {
    $prepage = 1;
}


if ($nowpageid == $page_count) {

    if ($sum <= $detailnum) //总数数据少于每页显示数目，就把循环改成总数数目
    {
        $detailnum = $sum;
        $nextpage = $nowpageid;
    } else {
        $nextpage = $nowpageid;
        $detailnum = fmod($sum, $detailnum);
        if ($detailnum == 0) //这个if是因为当数据正好30条，能除尽的话，最后一页就会因为fmod能整除的缘故，最后一页不显示数据
        {
            $detailnum = 5;
        }
    }
    //如果现在页数等于总页数（总页数上面求出，最后一页不够循环数的时候），那么最后一页的detailnum就会等于余下数据(不足一页)的数目.

}


?>







<!--开始搜索对应的detail资料-->
<!--搜索的数目和起始，会根据上面"翻页代码相关设置"的内容，进行搜搜-->
<?

//设置需要搜索的表格的前缀后缀
$table_Suffix = "detail";
$table = $table_Prefix.$table_Suffix;
//设置需要搜索的表格的前缀后缀


//设置需要搜索的表格的排序项和升降项
$sort_by = "nid";
$sort_order = "desc";
//设置需要搜索的表格的排序项和升降项


if ($_REQUEST ['sortby']) {
    $sort_by = $_REQUEST ['sortby'];
}


if ($_REQUEST ['sortorder']) {
    $sort_order = $_REQUEST ['sortorder'];
}




//加入有传递过来的id的话，就会出现这种情况
if ($id_where) {

	

	//设置需要搜索的表格的前缀后缀
    $table_Suffix = 'detail';
    $table = $table_Prefix . $table_Suffix;
	//设置需要搜索的表格的前缀后缀


    $relation_detail_str_1 = "select * from $table where 1 $where and nid in (".implode(',',$relation_id_list).")";

 /*   for ($i = 0; $i < count($relation_nid); $i++) {
        if ($i != 0) {
            $relation_detail_str_2 = $relation_detail_str_2 . ' or ';
        }

        $relation_detail_str_2 = $relation_detail_str_2 . "nid=" . $relation_nid[$i];


    }
*/

    $relation_detail_str_3 = $where." ORDER BY $sort_by $sort_order  limit $detailstar , $detailnum";


    $result = mysql_query($relation_detail_str_1 . $relation_detail_str_2 . $relation_detail_str_3);

	
} 
else {

    $result = mysql_query("select * from $table where belong = '$sys_belong' $where ORDER BY $sort_by $sort_order  limit $detailstar , $detailnum");
}


while ($row = mysql_fetch_array($result)) {
    $detail_nid[]           = $row['nid'];
    $detail_mid[]           = $row['mid'];
    $detail_name[]          = $row['name'];
    $detail_title[]         = $row['title'];
    $detail_keyword[]       = $row['keyword'];
    $detail_description[]   = $row['description'];
    $detail_model[]         = $row['model'];
    $detail_introduce[]     = $row['introduce'];
    $detail_detail[]        = $row['detail'];
    $detail_stock[]         = $row['stock'];
    $detail_price[]         = $row['price'];
    $detail_memberprice[]   = $row['memberprice'];
    $detail_discountprice[] = $row['discountprice'];
    $detail_local[]         = $row['local'];
    $detail_listorder[]     = $row['listorder'];
    $detail_hotsale[]       = $row['hotsale'];
    $detail_discount[]      = $row['discount'];
    $detail_recommend[]     = $row['recommend'];
    $detail_soldout[]       = $row['soldout'];
    $detail_publish[]       = $row['publish'];
    $detail_clickrate[]     = $row['clickrate'];
    $detail_onsaletime[]    = $row['onsaletime'];
    $detail_inputtime[]     = $row['inputtime'];
    $detail_modifytime[]    = $row['modifytime'];
    $detail_icon[]          = $row['icon'];
    $detail_banner[]        = $row['banner'];
    $detail_image1[]        = $row['image1'];
    $detail_image2[]        = $row['image2'];
    $detail_image3[]        = $row['image3'];
    $detail_image4[]        = $row['image4'];
    $detail_image5[]        = $row['image5'];
    $term0[]                = $row['term0'];
    $term1[]                = $row['term1'];
    $term2[]                = $row['term2'];
    $term3[]                = $row['term3'];
    $term4[]                = $row['term4'];
    $term5[]                = $row['term5'];
    $detail_mark[]          = $row['mark'];
    $detail_markorder[]     = $row['markorder'];
    $detail_link[]          = $row['link'];
    $detail_storage[]       = $row['storage'];
    $termorder[]            = $row['termorder'];
    $detail_attachment[]    = $row['attachment'];
    $detail_belong[]        = $row['belong'];
    $detail_url[]           = $row['url'];
    $detail_is_disabled[]           = $row['is_disabled'];

}
mysql_close($con);

?>








